package com.rc.evcharger.utils;

import com.rc.evcharger.common.utils.DataUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/8/8 9:34
 */
public class CarVinUtil {
    private Logger logger = LoggerFactory.getLogger(CarVinUtil.class);
    private static Map<String, String> carVinMap = new HashMap<String, String>();
    private static CarVinUtil carVinUtil = null;

    private CarVinUtil() {
    }

    public static CarVinUtil newInstance() {
        if (carVinUtil == null) {
            carVinUtil = new CarVinUtil();
        }
        return carVinUtil;
    }

    public String getVin(String rtua, String gunNum) {
        if (gunNum.length() < 2) {
            gunNum = "0" + gunNum;
        }
        String key = DataUtil.getKey(this.getClass().getName(), rtua, gunNum);
        return carVinMap.get(key);
    }

    public void addVin(String rtua, String gunNum, String vin) {
        if (gunNum.length() < 2) {
            gunNum = "0" + gunNum;
        }
        String key = DataUtil.getKey(this.getClass().getName(), rtua, gunNum);
        logger.info("添加vin码的key:" + key + " 添加vin码的vin:" + vin);
        carVinMap.put(key, vin);
    }

    /**
     * 删除双枪的vin码
     *
     * @param rtua
     */
    public void removeVin(String rtua) {
        String key = DataUtil.getKey(this.getClass().getName(), rtua, "01");
        if (carVinMap.containsKey(key)) {
            logger.info("删除vin码的key:" + key);
            carVinMap.remove(key);
        }
        key = DataUtil.getKey(this.getClass().getName(), rtua, "02");
        if (carVinMap.containsKey(key)) {
            logger.info("删除vin码的key:" + key);
            carVinMap.remove(key);
        }
    }

    public void removeVin(String rtua, String gunNum) {
        if (gunNum.length() < 2) {
            gunNum = "0" + gunNum;
        }
        String key = DataUtil.getKey(this.getClass().getName(), rtua, gunNum);
        if (carVinMap.containsKey(key)) {
            logger.info("删除vin码的key:" + key);
            carVinMap.remove(key);
        }

    }
}
